<?php
namespace app\common\model\mysql;

//use think\Model;
class Goods extends ModelBase{
	// 指定数据库表
	public $table = 'mall_goods';

	/*
	// 根据ID更新数据
	public function updateById($id,$data){
		$data['update_time'] = time();
		$where = [
			'id'=>$id,
		];
		return $this->where($where)->save($data);
	}
	*/

	public function searchTitleAttr($query,$value){
		$query->where('title','like','%'.$value.'%');
	}

	public function searchCreateTimeAttr($query,$value){
		$query->whereBetweenTime('create_time',$value[0],$value[1]);
	}

	public function getLists($likeKeys,$data,$num){
		$order = [
			'listorder' => 'desc',
			'id' => 'desc',
		];
		if(!empty($likeKeys)){
			$res = $this->withSearch($likeKeys,$data);
		}else{
			$res = $this;
		}
		$result = $res->whereIn('status',[0,1])
			->order($order)
			->paginate($num);
		//echo $this->getLastSql();exit;
		return $result;
	}

	//获取正常商品
	public function getNormalGoodsByCondition($data,$field=true,$limit=5){
		$data['status'] = config('status.success');
		$order = [
			'listorder'=>'desc',
			'id'=>'desc',
		];
		$result = $this->where($data)
			->field($field)
			->order($order)
			->limit($limit)
			->select();
		//echo $this->getLastSql();exit;
		return $result;
	}

	public function getImageAttr($value){
		return request()->domain().$value;
	}

	public function getCarouselImageAttr($value){
		if(!empty($value)){
			$value = explode(',', $value);
			$value = array_map(function($v){
				return request()->domain().$v;
			}, $value);
		}
		return $value;
	}

	public function getNormalGoodsFindInSetCategoryId($categoryId,$field){
		$where = [
			'status'=>config('status.success'),
		];
		$order = [
			'listorder'=>'desc',
			'id'=>'desc',
		];
		$result = $this->where($where)
			->whereFindInSet('category_path_id',$categoryId)
			->field($field)
			->order($order)
			->select();
		//echo $this->getLastSql();exit;
		return $result;
	}

	public function getNormalLists($data,$num=5,$field=true,$order){
		// $order = ['listorder'=>'desc','id'=>'desc'];
		$res = $this;
		if(isset($data['category_path_id'])){
			$res = $this->whereFindInSet('category_path_id',$data['category_path_id']);
		}
		$list = $res->where('status','=',config('status.success'))
			->order($order)
			->field($field)
			->paginate($num);
		//echo $this->getLastSql();exit;
		return $list;
	}
}